import { MediaPlayCore } from "./MediaPlayCore";
import { IMediaPlayCore } from "../../interfaces/IMediaPlayCore";
import { Log } from "../../../debug/Log";
import { MediaPlayEvent } from "../../events/MediaPlayEvent";
//class NetStreamPlayCore
    
    export  class NetStreamPlayCore extends MediaPlayCore implements IMediaPlayCore
    {
        constructor(arg1: string=null, arg2: number=1000, arg3: Boolean=false, arg4: number=250, arg5: Object=null)
        {
            super(arg1, arg2, arg5);
            this._timer = new Object /* flash.utils.Timer */(arg4);
            this._timer.addEventListener(flash.events.TimerEvent.TIMER, this.playingStateTimerEventHandler);
            this._nc = new Object /* flash.net.NetConnection */();
            this._nc.connect(null);
            this._nc.addEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, this.asyncErrorEventHandler);
            this._nc.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.securityErrorEventHandler);
            this._ns = new Object /* flash.net.NetStream */(this._nc);
            this._ns.bufferTime = arg2 / 1000;
            this._ns.checkPolicyFile = arg3;
            this._ns.client = {"onMetaData": this.onMetaDataHandler, "onCuePoint": this.onCuePointHandler};
            this._ns.addEventListener(flash.events.NetStatusEvent.NET_STATUS, this.netStatusEventHandler);
            this._ns.addEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, this.asyncErrorEventHandler);
            this._ns.addEventListener(flash.events.IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
            return;
        }

        public /* override */ set volume(arg1: number)
        {
            var loc1=null;
            super.volume = arg1;
            if (this._ns) 
            {
                loc1 = this._ns.soundTransform;
                loc1.volume = _mute ? 0 : arg1;
                this._ns.soundTransform = loc1;
            }
            return;
        }

        public /* override */ play(): void
        {
            var loc1;
            if (playing) 
            {
                Log.system("声音正处于播放状态，无法重复进行播放动作！");
            }
            else 
            {
                super.play();
                if (this._ns) 
                {
                    this._timer.start();
                    try 
                    {
                        this._ns.play(url);
                    }
                    catch (err)
                    {
                        Log.system("流播放安全性错误：" + err.message);
                    }
                    callbackFunction(MediaPlayEvent.OPEN);
                }
            }
            return;
        }

        public /* override */ pause(): void
        {
            if (playing) 
            {
                super.pause();
                this._ns.pause();
                callbackFunction(MediaPlayEvent.PAUSE);
            }
            else 
            {
                Log.system("声音正处于非播放状态，无法进行暂停播放动作！");
            }
            return;
        }

        public /* override */ resume(): void
        {
            if (paused) 
            {
                this._seekPause = false;
                super.play();
                this._ns.resume();
                callbackFunction(MediaPlayEvent.RESUME);
            }
            else 
            {
                Log.system("声音正处于非暂停状态，无法进行暂停播放动作！");
            }
            return;
        }

        public /* override */ stop(): void
        {
            var loc1;
            if (stopped) 
            {
                Log.system("声音正处于停止状态，无法重复进行停止播放动作！");
            }
            else 
            {
                super.stop();
                this._ns.pause();
                this._timer.stop();
                try 
                {
                    this._ns.close();
                }
                catch (err)
                {
                    Log.system("数据流关闭错误“" + err.message);
                }
                callbackFunction(MediaPlayEvent.STOP);
            }
            return;
        }

        public /* override */ seek(arg1: number=0): void
        {
            if (stopped) 
            {
                Log.system("声音正处于停止状态，无法进行播放进度搜索动作！");
            }
            else 
            {
                this._seekTime = arg1;
                this._seekPause = paused;
                this._ns.seek(arg1);
                callbackFunction(MediaPlayEvent.SEEK);
            }
            return;
        }

        public /* override */ seekPercent(arg1: number=0): void
        {
            var loc1=arg1 * _totalTime;
            this.seek(loc1);
            return;
        }

        public /* override */ dispose(): void
        {
            if (this._ns != null) 
            {
                this._ns.removeEventListener(flash.events.NetStatusEvent.NET_STATUS, this.netStatusEventHandler);
                this._ns.removeEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, this.asyncErrorEventHandler);
                this._ns.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
                this._ns = null;
            }
            if (this._timer != null) 
            {
                this._timer.removeEventListener(flash.events.TimerEvent.TIMER, this.playingStateTimerEventHandler);
                this._timer = null;
            }
            if (this._nc != null) 
            {
                this._nc.removeEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, this.asyncErrorEventHandler);
                this._nc.removeEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.securityErrorEventHandler);
                this._nc = null;
            }
            super.dispose();
            return;
        }

        /* internal  */securityErrorEventHandler(arg1: Object /* flash.events.SecurityErrorEvent */): void
        {
            callbackFunction(MediaPlayEvent.ERROR, arg1.text);
            return;
        }

        /* internal  */asyncErrorEventHandler(arg1: Object /* flash.events.AsyncErrorEvent */): void
        {
            callbackFunction(MediaPlayEvent.ERROR, arg1.text);
            return;
        }

        /* internal  */ioErrorEventHandler(arg1: Object /* flash.events.IOErrorEvent */): void
        {
            callbackFunction(MediaPlayEvent.IO_ERROR, arg1.text);
            return;
        }

        /* internal  */netStatusEventHandler(arg1: Object /* flash.events.NetStatusEvent */): void
        {
            var loc2=NaN;
            var loc1=arg1.info.code;
            var loc3=loc1;
            switch (loc3) 
            {
                case "NetStream.Play.Start":
                {
                    this._startPlay = true;
                    callbackFunction(MediaPlayEvent.PLAY);
                    break;
                }
                case "NetStream.Play.Stop":
                {
                    this.stop();
                    callbackFunction(MediaPlayEvent.PLAY_COMPLETE);
                    break;
                }
                case "NetStream.Buffer.Empty": case "NetStream.Seek.Notify":
                {
                    _buffering = true;
                    break;
                }
                case "NetStream.Buffer.Full":
                {
                    _buffering = false;
                    break;
                }
                case "NetStream.Seek.InvalidTime":
                {
                    loc2 = arg1.info.message.details as number;
                    this.seek(loc2);
                    break;
                }
                case "NetStream.Play.StreamNotFound": case "NetConnection.Connect.Failed": case "NetStream.Play.Failed": case "NetStream.Play.FileStructureInvalid": case "The MP4 doesn\'t contain any supported tracks":
                {
                    callbackFunction(MediaPlayEvent.ERROR, loc1);
                    break;
                }
            }
            return;
        }

        /* internal  */onMetaDataHandler(arg1: Object): void
        {
            _mediaData = arg1;
            callbackFunction(MediaPlayEvent.INIT);
            _totalTime = arg1.duration;
            if (this._ns.bufferTime > arg1.duration) 
            {
                var loc1;
                this._ns.bufferTime = loc1 = arg1.duration;
                _buffering = loc1;
            }
            return;
        }

        /* internal  */onCuePointHandler(arg1: Object): void
        {
            return;
        }

        /* internal  */playingStateTimerEventHandler(arg1: Object /* flash.events.TimerEvent */): void
        {
            var loc1=NaN;
            loc1 = 0;
            if (this._ns.bytesLoaded != this._ns.bytesTotal) 
            {
                _loadPercentage = this._ns.bytesLoaded / this._ns.bytesTotal;
                callbackFunction(MediaPlayEvent.PROGRESS);
            }
            else 
            {
                callbackFunction(MediaPlayEvent.COMPLETE);
            }
            if (_buffering) 
            {
                loc1 = this._ns.bufferLength / this._ns.bufferTime;
                _bufferPercentage = loc1 > 1 ? 1 : loc1;
                callbackFunction(MediaPlayEvent.BUFFERING);
            }
            else if (this._startPlay) 
            {
                _playheadTime = this._seekPause ? this._seekTime : this._ns.time;
                callbackFunction(MediaPlayEvent.PLAYING);
            }
            return;
        }

        private /* var */_nc: Object /* flash.net.NetConnection */;

        private /* var */_ns: Object /* flash.net.NetStream */;

        private /* var */_timer: Object /* flash.utils.Timer */;

        private /* var */_startPlay: Boolean=false;

        private /* var */_seekPause: Boolean=false;

        private /* var */_seekTime: number=0;
    }
